package com.rainng.coursesystem.service.admin;

import com.rainng.coursesystem.manager.admin.EventManager;
import com.rainng.coursesystem.model.bo.EventItemBO;
import com.rainng.coursesystem.model.entity.EventEntity;
import com.rainng.coursesystem.model.vo.response.ResultVO;
import com.rainng.coursesystem.model.vo.response.table.EventItemVO;
import com.rainng.coursesystem.service.BaseService;
import com.rainng.coursesystem.util.LessonTimeConverter;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class EventService extends BaseService {
    private final EventManager manager;
    private final LessonTimeConverter lessonTimeConverter;

    public EventService(EventManager manager, LessonTimeConverter lessonTimeConverter) {
        this.manager = manager;
        this.lessonTimeConverter = lessonTimeConverter;
    }

    public ResultVO getPageCount(String areaName, String leaderName, String name) {
        return result(manager.getPageCount(areaName, leaderName, name));
    }

    public ResultVO getPage(Integer index, String areaName, String leaderName, String name) {
        List<EventItemBO> boList = manager.getPage(index, areaName, leaderName, name);
        List<EventItemVO> voList = new ArrayList<>(boList.size());

        for (EventItemBO bo : boList) {
            EventItemVO vo = new EventItemVO();
            BeanUtils.copyProperties(bo, vo);
            vo.setTime(lessonTimeConverter.covertTimePart(bo.getTime()));
            voList.add(vo);
        }

        return result(voList);
    }

    public ResultVO get(Integer id) {
        EventEntity entity = manager.get(id);
        if (entity == null) {
            return failedResult("项目Id: " + id + "不存在!");
        }

        return result(entity);
    }

    public ResultVO update(EventEntity entity) {
        EventEntity origin = manager.get(entity.getId());
        if (origin == null) {
            return failedResult("项目Id: " + entity.getId() + "不存在!");
        }
        if (manager.getLeaderById(entity.getLeaderId()) == null) {
            return failedResult("带队团长Id: " + entity.getLeaderId() + "不存在!");
        }

        entity.setSelectedCount(origin.getSelectedCount());

        manager.update(entity);
        return result("更新成功");
    }

    public ResultVO delete(Integer id) {
        if (manager.get(id) == null) {
            return failedResult("项目Id: " + id + "不存在!");
        }
        if (manager.hasMemberEvent(id)) {
            return failedResult("还有团员未退选此项目");
        }

        manager.delete(id);
        return result("删除成功");
    }

    public ResultVO create(EventEntity entity) {
        if (manager.get(entity.getId()) != null) {
            return failedResult("项目Id: " + entity.getId() + "已存在!");
        }
        if (manager.getLeaderById(entity.getLeaderId()) == null) {
            return failedResult("带队团长Id: " + entity.getLeaderId() + "不存在!");
        }

        manager.create(entity);
        return result("添加成功");
    }

    public ResultVO listName() {
        return result(manager.listName());
    }
}
